package org.example.struct.array;

/**
 * @author liyishan
 * @date 2023/8/16 13:29
 * @apiNote
 */

public class Test00999NumRookCaptures {
    public static void main(String[] args) {
        char[][] board = new char[][]{{'.','.','.','.','.','.','.','.'},
                {'.','.','.','p','.','.','.','.'},
                {'.','.','.','R','p','.','.','.'},
                {' ',' ',' ',' ',' ',' ',' ',' '},
                {' ',' ',' ',' ',' ',' ',' ',' '},
                {' ',' ',' ','p',' ',' ',' ',' '},
                {' ',' ',' ',' ',' ',' ',' ',' '},
                {'.','.','.','.','.','.','.','.'}};
        System.out.println(numRookCaptures(board));
    }
    public static int numRookCaptures(char[][] board) {
        int cnt = 0,st = 0,en = 0;
        //         上 右 左 下
        int[] dx = {0,1,0,-1};
        int[] dy = {1,0,-1,0};
        for(int i = 0;i < 8;i++){
            for(int j = 0;j<8;j++){
                if(board[i][j] == 'R'){
                    st = i;
                    en = j;
                    break;
                }
            }
        }
        for(int i = 0;i<4;i++){
            for(int step = 0;;step++){
                int tx = st+step*dx[i];
                int ty = en+step*dy[i];
                if(tx < 0 ||tx >= 8 ||ty <0 || ty>= 8 || board[tx][ty] == 'B'){
                    break;
                }
                if(board[tx][ty] == 'p'){
                    cnt++;
                    break;
                }
            }
        }
        return cnt;
    }
}
